package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.Constant;
import com.ruoyi.common.Utils;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ShopMapper;
import com.ruoyi.system.domain.Shop;
import com.ruoyi.system.service.IShopService;

import javax.annotation.Resource;

/**
 * 配送店Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-09
 */
@Service
public class ShopServiceImpl implements IShopService {
    @Resource
    private ShopMapper shopMapper;

    @Resource
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询配送店
     *
     * @param id 配送店主键
     * @return 配送店
     */
    @Override
    public Shop selectShopById(Long id) {

        Shop shop = shopMapper.selectShopById(id);
        if (shop != null) {

            List<String> addCode = new ArrayList<>();
            addCode.add(shop.getProvinceCode());
            addCode.add(shop.getCityCode());
            addCode.add(shop.getDistrictCode());
            shop.setAddressCode(addCode);

            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(Constant.CONDE_SYS_SHOP_TYPE);
            HashMap<String, SysDictData> dictDataHashMap = Utils.code2Map(dictDatas);
            if(dictDataHashMap.containsKey(shop.getAddressTag())){
                shop.setTag(dictDataHashMap.get(shop.getAddressTag()).getDictLabel());
            }

        }
        return shop;
    }

    /**
     * 查询配送店列表
     *
     * @param sp 配送店
     * @return 配送店
     */
    @Override
    public List<Shop> selectShopList(Shop sp) {
        List<Shop> shops = shopMapper.selectShopList(sp);
        if (shops != null) {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(Constant.CONDE_SYS_SHOP_TYPE);
            HashMap<String, SysDictData> dictDataHashMap = Utils.code2Map(dictDatas);
            List<String> addCode = null;
            for (int i = 0; i < shops.size(); i++) {
                Shop shop = shops.get(i);
                addCode = new ArrayList<>();
                if (shop != null) {
                    addCode.add(shop.getProvinceCode());
                    addCode.add(shop.getCityCode());
                    addCode.add(shop.getDistrictCode());
                    shop.setAddressCode(addCode);
                    if(dictDataHashMap.containsKey(shop.getAddressTag())){
                        shop.setTag(dictDataHashMap.get(shop.getAddressTag()).getDictLabel());
                    }
                }
            }
        }

        return shops;
    }

    /**
     * 新增配送店
     *
     * @param shop 配送店
     * @return 结果
     */
    @Override
    public int insertShop(Shop shop) {
        shop.setCreateTime(DateUtils.getNowDate());
        return shopMapper.insertShop(shop);
    }

    /**
     * 修改配送店
     *
     * @param shop 配送店
     * @return 结果
     */
    @Override
    public int updateShop(Shop shop) {
        shop.setUpdateTime(DateUtils.getNowDate());
        return shopMapper.updateShop(shop);
    }

    /**
     * 批量删除配送店
     *
     * @param ids 需要删除的配送店主键
     * @return 结果
     */
    @Override
    public int deleteShopByIds(Long[] ids) {
        return shopMapper.deleteShopByIds(ids);
    }

    /**
     * 删除配送店信息
     *
     * @param id 配送店主键
     * @return 结果
     */
    @Override
    public int deleteShopById(Long id) {
        return shopMapper.deleteShopById(id);
    }
}
